package com.zds.e_exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @className:CustionSimpleMappingExceptionHandler
 * @date: 2025/1/17 下午2:52
 * @description:当发生异常时，Spring MVC 将调用 SimpleMappingExceptionHandler 的 doResolveException 方法。
 * 你可以根据异常类型来决定要返回的视图，比如将 NullPointerException 映射到 "null-pointer-error" 视图，
 * 将 ArithmeticException 映射到 "arithmetic-error" 视图等。
 */
public class CustomSimpleMappingExceptionHandler extends SimpleMappingExceptionResolver {
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        if (ex instanceof Exception){
            modelAndView.setViewName("error");
            modelAndView.setStatus(HttpStatus.NOT_FOUND);
            modelAndView.addObject("exception", ex);
        }else if (ex instanceof IllegalArgumentException){

        }else if (ex instanceof RuntimeException){
        }
        return modelAndView;
    }
}
